package com.monkeygroup.mygoplus.ecchat.ui.contact;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.ecchat.common.utils.ToastUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/21.
 */
public class AddFriendSearchActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout search_result_item;
    private ImageView head_icon;
    private TextView item_name, item_sign;
    private EditText friend_search_edit_id;
    private String userMobile = "", mobile = "";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_search_friend);
    }

    @Override
    public void initView() {
        friend_search_edit_id = (EditText) findViewById(R.id.friend_search_edit_id);
        search_result_item = (RelativeLayout) findViewById(R.id.search_result_item);
        search_result_item.setVisibility(View.GONE);
        head_icon = (ImageView) findViewById(R.id.head_icon);
        item_name = (TextView) findViewById(R.id.item_name);
        item_sign = (TextView) findViewById(R.id.item_sign);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
        findViewById(R.id.search_confirm).setOnClickListener(this);
        findViewById(R.id.add_icon).setOnClickListener(this);
    }

    @Override
    public void doEvent() {

    }

    /**
     * 獲取用戶信息
     **/
    private void getUserDetail() {
        String requestUrl = RequestUrl.baseUrl + RequestUrl.getUserDetail;
        HttpModel httpModel = new HttpModel(requestUrl, getUserParams(), new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    search_result_item.setVisibility(View.VISIBLE);
                    Map<String, String> info = (Map) data;
                    if (info.get("username") != null && !info.get("username").equals("")) {
                        item_name.setText(info.get("username"));
                    } else if (info.get("userID") != null && !info.get("userID").equals("")) {
                        item_name.setText(info.get("userID"));
                    } else {
                        item_name.setText(info.get("mobile"));
                    }
                    mobile = info.get("mobile");
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    /**
     * 發送添加好友請求
     **/
    private void addFriend() {
        String requestUrl = RequestUrl.baseUrl + RequestUrl.requestAddFriend;
        HttpModel httpModel = new HttpModel(requestUrl, getParams(), new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (VilidateUtil.isSuccess(resultCode)) {
                    Map<String, String> map = (Map) data;
                    ToastUtil.showMessage(map.get("message"));
                } else {
                    ToastUtil.showMessage(resultMsg);
                }
            }
        }, true);

        httpModel.startRequest();
    }

    private Map<String, String> getUserParams() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", userMobile);

        return params;
    }

    private Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", MyGoApplication.myECUserid);
        if (mobile.equals("")) {
            mobile = friend_search_edit_id.getText().toString().trim();
        }
        params.put("friendMobile", mobile);

        return params;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.search_confirm:
                userMobile = friend_search_edit_id.getText().toString().trim();
                if (userMobile != null && !userMobile.equals("")) {
                    getUserDetail();

                } else {
                    ToastUtil.showMessage("ID或手機號碼不能為空");
                }
                break;
            case R.id.add_icon:
                addFriend();
                break;
            default:
                break;
        }
    }
}
